package com.sw.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.sw.model.Commodity;
import com.sw.model.ShoppingCart;
import com.sw.service.CommodityService;
import com.sw.service.ShoppingCartService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping("/ShoppingCart")
public class ShoppingCartController {

	@Autowired
	private ShoppingCartService shoppingCartService;
	
	@Autowired
	private CommodityService commodityService;
	
	@RequestMapping(value="/ShoppingCarts",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public @ResponseBody Object ShoppingCarts(){
		List<ShoppingCart> ShoppingCarts = shoppingCartService.getShoppingCartAll();
		return ShoppingCarts;
	}
	
	/**
	 * 获取商品
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getCommodity/{id}")
	public @ResponseBody Object commodityPay(@PathVariable Integer id){
		Commodity commodity = commodityService.getCommodityById(id);
		
		return commodity;
	}
	
	
	
	@RequestMapping("/pay")
	public @ResponseBody  String pay(HttpServletRequest request,HttpServletResponse response) throws AlipayApiException, IOException{
		
		
		/*AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2016091801918336","MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALNCqYIqEkwNdBGNnwloxej+pMZXbAhvtZ8cNBoGIeEdeOPJXK+PyhumUkXeu+TqRnsSVWWtnNUOoWD2Keky8ShVlpq43nK7CkSK7dKjlWI7GeZY8IaMUoqqLOE4Vp87zePaubXFVDvqzR3Ahn6FXDRHY7ZSADTa5oY0K6uJ/CbfAgMBAAECgYEAiRBJh6sffmMzAlDk707bNIGv/6/MfM7dciEgfV1+/h+I1bKbWel5wZZ0hdP+mtO+vE+SWEJdDlM3dWoDNbmrxcFI0Q3e//c1pb3gjv6/V/R9PBmrctYyJZlTHYlPC0Cm9/HvZN+5wjtThFikvyO5EQsLoPNqS3lRVmluOACqaMECQQDiHYt5hBGswMB+8+cfnehUyZEqOgNz3gTc6Ynruj3NljY7GthVCHVY6JxKVFSjNvN+aafdTCehK0SAu6/YGamPAkEAyvPPpm1X+0jYyHpPFB0fyC+8X/QXK28uERAYgqs2q/iXmaLwCf60go6wv2DjsmNE/n4jbVchBNItCYRPCN/lsQJAY+Wdqy/4BxSZdv0WzVccP+lVowxCXjxmdZsP/f8oMt38U5yGmvrDrWYHNqePRRSP+vd+3Dw6/e95sk45YAXmbQJAKnjZhNevKNw22cWW1Pu/UfKDdoUrcCCFQHbg2j+gYl7UwwcxOrY96nCYFoTdQdC6eJPHMDOiqlKdsDcrHw1UEQJBAM9PTgUvjKvMKplVdRj/oWokf+HcUjDAq4DZvPI8LwbTI7nc7VIIRapELni94ssQGQrnfJcZhmNihgU/RKCy3QA=","json","UTF-8","MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB");
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
		 alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
		    alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
		    alipayRequest.setBizContent("{" +
		        "    \"out_trade_no\":\"20150320010101002\"," +
		        "    \"total_amount\":88.88," +
		        "    \"subject\":\"Iphone6 16G\"," +
		        "    \"seller_id\":\"2088123456789012\"," +
		        "    \"product_code\":\"QUICK_WAP_PAY\"" +
		        "  }");//填充业务参数
		    String form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
		    response.setContentType("text/html;charset=UTF-8");
		    response.getWriter().write(form);//直接将完整的表单html输出到页面
		    response.getWriter().flush();*/
		 
		
		 JSONObject jsonObject = new JSONObject();  
		
		 jsonObject.put("msg", "success");
		
		 
	     return jsonObject.toString();
	}
}
